#include "Globals.h"
#include "cScene.h"
#include <list>

cScene::cScene(void) {}
cScene::~cScene(void){}

void cScene::Init()
{
	MakeCubeDL((float)TILE_SIZE,(float)TILE_SIZE*SCENE_HEIGHT,(float)TILE_SIZE,1.0f,1.0f,1.0f);
	MakeCubeDL2((float)TILE_SIZE,(float)TILE_SIZE*SCENE_HEIGHT,(float)TILE_SIZE,1.0f,1.0f,1.0f);
	MakeCubeDL3((float)TILE_SIZE,(float)TILE_SIZE*SCENE_HEIGHT*0.95,(float)TILE_SIZE,1.0f,1.0f,1.0f);
	MakeFace((float)TILE_SIZE,(float)TILE_SIZE*SCENE_HEIGHT,(float)TILE_SIZE,1.0f,1.0f,1.0f);
	MakeFloor((float)TILE_SIZE,(float)TILE_SIZE*SCENE_HEIGHT,(float)TILE_SIZE,1.0f,1.0f,1.0f);
	doorRotate = 0;
	openingDoor = false;
	out = false;
}

bool cScene::LoadLevel(int level)
{
	FILE *fd;
	char file[16],tile;
	int i,j;

	if(level<10) sprintf(file,"%s0%d%s",(char *)FILENAME,level,(char *)FILENAME_EXT);
	else		 sprintf(file,"%s%d%s",(char *)FILENAME,level,(char *)FILENAME_EXT);

	fd=fopen(file,"r");
	if(fd==NULL) return false;

	for(i=SCENE_DEPTH-1;i>=0;i--)
	{
		for(j=0;j<SCENE_WIDTH;j++)
		{
			fscanf(fd,"%c",&tile);
			if(tile==' ')
			{
				//Tiles must be != 0 !!!
				map[(i*SCENE_WIDTH)+j]=0;
			}
			else
			{
				//Tiles = 1,2,3,...
				map[(i*SCENE_WIDTH)+j] = tile-48;
			}
		}
		fscanf(fd,"%c",&tile); //pass enter
	}

	fclose(fd);

	return true;
}
void cScene::Draw(cData *Data)
{
	if (openingDoor && doorRotate < M_PI/2 - 0.1) doorRotate += 0.01;
	int i,j,x,z;
	float w,d,tw,td;
	glEnable(GL_CULL_FACE); //Enable backface culling
    glCullFace(GL_BACK); 
	glEnable(GL_TEXTURE_2D);
	
	//Walls
	for(i=0;i<SCENE_DEPTH;i++)
	{
		x = 0;
		z = i*TILE_SIZE;

		for(j=0;j<SCENE_WIDTH;j++)
		{
			glPushMatrix();
				glTranslatef(x,0,-z);
				GLint val, aux;
				GLint loc = glGetUniformLocation(programID, "texture");
				switch(map[(i*SCENE_WIDTH)+j])
				{
					case 0:	break;
					case 1: //glUniform1i(loc, Data->GetID(IMG_WALL));
							glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_WALL));
							glCallList(dl_cube);
							//glGetUniformiv(programID, loc, &val);
							break;
					case 2: //glUniform1i(loc, Data->GetID(IMG_WALL));
							glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_WALL));
							glCallList(dl_cube);
							break;
					case 3: //glUniform1i(loc, Data->GetID(IMG_WALL2));
							glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_WALL2));
							glCallList(dl_cube);
							glGetUniformiv(programID, loc, &val);
							aux = val;
							break;
					case 4: //glUniform1i(loc, Data->GetID(IMG_DOOR));
							DrawDoor(Data);
							++j;
							x+=TILE_SIZE;
							//glCallList(dl_cube);
							break;
					case -4: //glUniform1i(loc, Data->GetID(IMG_DOOR));
							DrawDoor(Data);
							++j;
							x+=TILE_SIZE;
							//glCallList(dl_cube);
							break;
					case 5: //glUniform1i(loc, Data->GetID(IMG_WALL));
							glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_WALL));
							glCallList(dl_cube2);
							//glUniform1i(loc, Data->GetID(IMG_BOOKS));
							glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_BOOKS));
							glCallList(dl_face);
							break;
					case 6: //glUniform1i(loc, Data->GetID(IMG_WIN1));
							glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_WIN1));
							glCallList(dl_cube);
							break;
					case 7: //glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_LAND));
							//glCallList(dl_floor);
							break;
				}
			glPopMatrix();
			x += TILE_SIZE;
		}
	}

	tw = (float)SCENE_WIDTH;
	td = (float)SCENE_DEPTH;
	w  = (float)SCENE_WIDTH*TILE_SIZE;
	d  = (float)SCENE_DEPTH*TILE_SIZE;
	//Floor
	if(!out)glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_FLOOR));
	else glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_LAND));
	glBegin(GL_QUADS);
		// Bottom Face
		glTexCoord2f(  tw,   td); glVertex3f(0, 0, 0);
		glTexCoord2f(0.0f,   td); glVertex3f(w, 0, 0);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0,-d);
		glTexCoord2f(  tw, 0.0f); glVertex3f(0, 0,-d);
	glEnd();
	//Roof
	if(!out){
		glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_ROOF));
		glBegin(GL_QUADS);
			// Top Face
			glTexCoord2f(0.0f,   td); glVertex3f(0, TILE_SIZE*SCENE_HEIGHT, 0);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, TILE_SIZE*SCENE_HEIGHT,-d);
			glTexCoord2f(  tw, 0.0f); glVertex3f(w, TILE_SIZE*SCENE_HEIGHT,-d);
			glTexCoord2f(  tw,   td); glVertex3f(w, TILE_SIZE*SCENE_HEIGHT, 0);
		glEnd();
	}

	glDisable(GL_TEXTURE_2D);
	glDisable(GL_CULL_FACE); 

	MakeSkyBox(Data);
	
}

void cScene::MakeCubeDL(float w,float h,float d,float tw,float th,float td)
{
	dl_cube = glGenLists(1);
	glNewList(dl_cube,GL_COMPILE);
		glBegin(GL_QUADS);
			// Front Face
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0,  0);
			glTexCoord2f(  tw, 0.0f); glVertex3f(w, 0,  0);
			glTexCoord2f(  tw,   th); glVertex3f(w, h,  0);
			glTexCoord2f(0.0f,   th); glVertex3f(0, h,  0);
			// Back Face
			glTexCoord2f(  tw, 0.0f); glVertex3f(0, 0, -d);
			glTexCoord2f(  tw,   th); glVertex3f(0, h, -d);
			glTexCoord2f(0.0f,   th); glVertex3f(w, h, -d);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0, -d);
			// Right face
			glTexCoord2f(  td, 0.0f); glVertex3f(w, 0, -d);
			glTexCoord2f(  td,   th); glVertex3f(w, h, -d);
			glTexCoord2f(0.0f,   th); glVertex3f(w, h,  0);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0,  0);
			// Left Face
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, -d);
			glTexCoord2f(  td, 0.0f); glVertex3f(0, 0,  0);
			glTexCoord2f(  td,   th); glVertex3f(0, h,  0);
			glTexCoord2f(0.0f,   th); glVertex3f(0, h, -d);
			// Bottom Face
			/*glTexCoord2f(  tw,   td); glVertex3f(0, 0, -d);
			glTexCoord2f(0.0f,   td); glVertex3f(w, 0, -d);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0,  0);
			glTexCoord2f(  tw, 0.0f); glVertex3f(0, 0,  0);*/
			// Top Face
			glTexCoord2f(0.0f,   td); glVertex3f(0, h, -d);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, h,  0);
			glTexCoord2f(  tw, 0.0f); glVertex3f(w, h,  0);
			glTexCoord2f(  tw,   td); glVertex3f(w, h, -d);
		glEnd();
	glEndList();
}

void cScene::MakeCubeDL2(float w,float h,float d,float tw,float th,float td)
{
	dl_cube2 = glGenLists(2);
	glNewList(dl_cube2,GL_COMPILE);
		glBegin(GL_QUADS);
			// Front Face
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0,  0);
			glTexCoord2f(  tw, 0.0f); glVertex3f(w, 0,  0);
			glTexCoord2f(  tw,   th); glVertex3f(w, h,  0);
			glTexCoord2f(0.0f,   th); glVertex3f(0, h,  0);
			// Back Face
			glTexCoord2f(  tw, 0.0f); glVertex3f(0, 0, -d);
			glTexCoord2f(  tw,   th); glVertex3f(0, h, -d);
			glTexCoord2f(0.0f,   th); glVertex3f(w, h, -d);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0, -d);
			// Right face
			/*glTexCoord2f(  td, 0.0f); glVertex3f(w, 0, -d);
			glTexCoord2f(  td,   th); glVertex3f(w, h, -d);
			glTexCoord2f(0.0f,   th); glVertex3f(w, h,  0);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0,  0);*/
			// Left Face
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, -d);
			glTexCoord2f(  td, 0.0f); glVertex3f(0, 0,  0);
			glTexCoord2f(  td,   th); glVertex3f(0, h,  0);
			glTexCoord2f(0.0f,   th); glVertex3f(0, h, -d);
			// Top Face
			glTexCoord2f(0.0f,   td); glVertex3f(0, h, -d);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, h,  0);
			glTexCoord2f(  tw, 0.0f); glVertex3f(w, h,  0);
			glTexCoord2f(  tw,   td); glVertex3f(w, h, -d);
		glEnd();
	glEndList();
}

void cScene::MakeFace(float w,float h,float d,float tw,float th,float td)
{
	dl_face = glGenLists(3);
	glNewList(dl_face,GL_COMPILE);
		glBegin(GL_QUADS);
			//Right face
			glTexCoord2f(  td, 0.0f); glVertex3f(w, 0, -d);
			glTexCoord2f(  td,   th); glVertex3f(w, h, -d);
			glTexCoord2f(0.0f,   th); glVertex3f(w, h,  0);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0,  0);
		glEnd();
	glEndList();
}

void cScene::MakeFloor(float w,float h,float d,float tw,float th,float td)
{
	tw = (float)SCENE_WIDTH;
	td = (float)SCENE_DEPTH;
	w  = (float)SCENE_WIDTH*TILE_SIZE;
	d  = (float)SCENE_DEPTH*TILE_SIZE;
	dl_floor = glGenLists(4);
	glNewList(dl_floor,GL_COMPILE);
		glBegin(GL_QUADS);
			// Bottom Face
			glTexCoord2f(  tw,   td); glVertex3f(0, 0, 0);
			glTexCoord2f(0.0f,   td); glVertex3f(w, 0, 0);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0,-d);
			glTexCoord2f(  tw, 0.0f); glVertex3f(0, 0,-d);
		glEnd();
	glEndList();
}

void cScene::MakeCubeDL3(float w,float h,float d,float tw,float th,float td)
{
	dl_cube3 = glGenLists(5);
	glNewList(dl_cube3,GL_COMPILE);
		glBegin(GL_QUADS);
			// Front Face
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0,  0);
			glTexCoord2f(  tw, 0.0f); glVertex3f(w, 0,  0);
			glTexCoord2f(  tw,   th); glVertex3f(w, h,  0);
			glTexCoord2f(0.0f,   th); glVertex3f(0, h,  0);
			// Back Face
			glTexCoord2f(  tw, 0.0f); glVertex3f(0, 0, -d);
			glTexCoord2f(  tw,   th); glVertex3f(0, h, -d);
			glTexCoord2f(0.0f,   th); glVertex3f(w, h, -d);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0, -d);
			// Right face
			glTexCoord2f(  td, 0.0f); glVertex3f(w, 0, -d);
			glTexCoord2f(  td,   th); glVertex3f(w, h, -d);
			glTexCoord2f(0.0f,   th); glVertex3f(w, h,  0);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0,  0);
			// Left Face
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, -d);
			glTexCoord2f(  td, 0.0f); glVertex3f(0, 0,  0);
			glTexCoord2f(  td,   th); glVertex3f(0, h,  0);
			glTexCoord2f(0.0f,   th); glVertex3f(0, h, -d);
			// Bottom Face
			/*glTexCoord2f(  tw,   td); glVertex3f(0, 0, -d);
			glTexCoord2f(0.0f,   td); glVertex3f(w, 0, -d);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(w, 0,  0);
			glTexCoord2f(  tw, 0.0f); glVertex3f(0, 0,  0);*/
			// Top Face
			glTexCoord2f(0.0f,   td); glVertex3f(0, h, -d);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, h,  0);
			glTexCoord2f(  tw, 0.0f); glVertex3f(w, h,  0);
			glTexCoord2f(  tw,   td); glVertex3f(w, h, -d);
		glEnd();
	glEndList();
}

int* cScene::GetMap(){
	return map;
}

void cScene::MakeSkyBox(cData *Data){
	float h = (float)SKYBOX_SIZE*TILE_SIZE;
	float w = (float)SKYBOX_SIZE*TILE_SIZE;
	float d = (float)SKYBOX_SIZE*TILE_SIZE;
	glEnable(GL_TEXTURE_2D);
	
	glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_SKYFRONT));
	glBegin(GL_QUADS);
		// Front Face
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-w, -2*h,  d);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 2*w, -2*h,  d);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 2*w,  h,  d);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-w,  h,  d);
	glEnd();
	glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_SKYBACK));
	glBegin(GL_QUADS);
		// Back Face
		glTexCoord2f(  0.0f, 1.0f); glVertex3f(-w, -2*h, -2*d);
		glTexCoord2f(  0.0f,   0.0f); glVertex3f(-w,  h, -2*d);
		glTexCoord2f(1.0f,   0.0f); glVertex3f( 2*w,  h, -2*d);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 2*w, -2*h, -2*d);
	glEnd();
	glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_SKYRIGHT));
	glBegin(GL_QUADS);
		// Right face
		glTexCoord2f(  0.0f, 1.0f); glVertex3f( 2*w, -2*h, -2*d);
		glTexCoord2f(  0.0f,   0.0f); glVertex3f( 2*w,  h, -2*d);
		glTexCoord2f(1.0f,   0.0f); glVertex3f( 2*w,  h,  d);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 2*w, -2*h,  d);
	glEnd();
	glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_SKYLEFT));
	glBegin(GL_QUADS);
		// Left Face
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-w, -2*h, -2*d);
		glTexCoord2f(  0.0f, 1.0f); glVertex3f(-w, -2*h,  d);
		glTexCoord2f(  0.0f,   0.0f); glVertex3f(-w,  h,  d);
		glTexCoord2f(1.0f,   0.0f); glVertex3f(-w,  h, -2*d);
	glEnd();	
	glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_SKYBOTTOM));
	glBegin(GL_QUADS);
		// Bottom Face
		glTexCoord2f(1.0f,   1.0f); glVertex3f(-w, -2*h, -2*d);
		glTexCoord2f(0.0f,   1.0f); glVertex3f( 2*w, -2*h, -2*d);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 2*w, -2*h,  d);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-w, -2*h,  d);
	glEnd();
	glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_SKYTOP));
	glBegin(GL_QUADS);
		// Top Face
		glTexCoord2f(0.0f,   1.0f); glVertex3f(-w,  h, -2*d);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-w,  h,  d);
		glTexCoord2f(  1.0f, 0.0f); glVertex3f( 2*w,  h,  d);
		glTexCoord2f(  1.0f,   1.0f); glVertex3f( 2*w,  h, -2*d);
	glEnd();
	glDisable(GL_TEXTURE_2D);
}

void cScene::DrawDoor(cData *Data){
	float td = 1.0f;
	float th = 1.0f;
	float tw = 1.0f;
	float w = (float)TILE_SIZE*2;
	float h = (float)TILE_SIZE*SCENE_HEIGHT;
	float d = (float)TILE_SIZE;
	
	glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_DOOR));
	glBegin(GL_QUADS);	
	// Front Face
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0,  0);
			glTexCoord2f(  tw, 0.0f); glVertex3f((w)*cos(doorRotate), 0,  -d*sin(doorRotate));
			glTexCoord2f(  tw,   th); glVertex3f((w)*cos(doorRotate), h,  -d*sin(doorRotate));
			glTexCoord2f(0.0f,   th); glVertex3f(0, h,  0);
	glEnd();
	/*glBindTexture(GL_TEXTURE_2D,Data->GetID(IMG_DOOR2));
	glBegin(GL_QUADS);
			// Front Face
			glTexCoord2f(0.0f, 0.0f); glVertex3f((w/2)*cos(doorRotate), 0,  -d*sin(doorRotate));
			glTexCoord2f(  tw, 0.0f); glVertex3f(w, 0,  0);
			glTexCoord2f(  tw,   th); glVertex3f(w, h,  0);
			glTexCoord2f(0.0f,   th); glVertex3f((w/2)*cos(doorRotate), h,  -d*sin(doorRotate));
	glEnd();*/
}

void cScene::SetOpeningDoor(bool b){
	this->openingDoor = b;
}

void cScene::SetOut(bool b){
	this->out = b;
}

void cScene::Dijkstra(int txini, int tzini, int txfi, int tzfi, int &nextX, int &nextZ){
	//if (txini == txfi && tzini == tzfi) return;
	//if (abs(txini-txfi) == 1 && tzini == tzfi) return;
	//if (abs(tzini-tzfi) == 1 && txini == txfi) return;
	if (abs(tzini-tzfi)+abs(txini-txfi) >= 20) return;
	int size = 10;
	std::list<int> l;
	for (int i = 0; i < SCENE_WIDTH; ++i)
		for (int j = 0; j < SCENE_DEPTH; ++j) {
			dist[i][j] = 9999.0f;
			visitat[i][j] = true;
		}
	for (int i = max(0,txini-8); i < min(SCENE_WIDTH,txini+8); ++i)
		for (int j = max(0,tzini-8); j < min(SCENE_DEPTH,tzini+8); ++j) {
			if (map[j + (i*SCENE_WIDTH) ] > 0){
				visitat[i][j] = true;
			}
			else{
				visitat[i][j] = false;
				if (i != txini && j != tzini)l.push_back(i*100+j);
			}
		}
	dist[txini][tzini] = 0;
	l.push_back(txini*100+tzini);
	int nodeActualX = txini;
	int nodeActualZ = tzini;
	
	while(!l.empty()){
		int value;
		int min = 99999;
        for(std::list<int>::iterator itList = l.begin(); itList != l.end(); itList++){
            if(min >= dist[(*itList)/100][(*itList)%100]){
                min = dist[(*itList)/100][(*itList)%100];
                nodeActualX = (*itList)/100;
				nodeActualZ = (*itList)%100;
            }
		}
		l.remove(nodeActualX*100+nodeActualZ);
		if (nodeActualX >= 0 && nodeActualZ >= 0 && nodeActualX < SCENE_WIDTH && nodeActualZ < SCENE_DEPTH){
			visitat[nodeActualX][nodeActualZ] = true;
			float distmin = 9999;
			int idMin = -1;
			float aux1 = dist[nodeActualX][nodeActualZ] +1.0f;
			float aux2 = dist[nodeActualX][nodeActualZ] +sqrt(2.0f);
			if(nodeActualX-1 >= 0){
				UpdateDist(nodeActualX-1, nodeActualZ, aux1, distmin, idMin);
				if (nodeActualZ-1 >= 0)   UpdateDist(nodeActualX-1, nodeActualZ-1, aux2, distmin, idMin);
				if (nodeActualZ+1 < SCENE_DEPTH)  UpdateDist(nodeActualX-1, nodeActualZ+1, aux2, distmin, idMin);
			}
			if(nodeActualX+1 < SCENE_WIDTH){
				UpdateDist(nodeActualX+1, nodeActualZ, aux1, distmin, idMin);
				if (nodeActualZ-1 >= 0)  UpdateDist(nodeActualX+1, nodeActualZ-1, aux2, distmin, idMin);
				if (nodeActualZ+1 < SCENE_DEPTH)  UpdateDist(nodeActualX+1, nodeActualZ+1, aux2, distmin, idMin);
			}
			if (nodeActualZ-1 >= 0){
				UpdateDist(nodeActualX, nodeActualZ-1, aux1, distmin, idMin);
			}
			if (nodeActualZ+1 < SCENE_DEPTH){
				UpdateDist(nodeActualX, nodeActualZ+1, aux1, distmin, idMin);
			}
		}
	}
	int aux = 0;

	bool final = false;
	float min = 9999.0f;
	int tx, tz;
	if (dist[txfi][tzfi] >= 99){
		txfi = txini+2;
		tzfi = tzini+2;
	}
	tx = txfi; tz = tzfi;
	int iteracions = 0;
	while (!final && iteracions < SCENE_WIDTH*SCENE_DEPTH){
		nextX = txfi;
		nextZ = tzfi;
		if(txfi-1 >= 0 && !final){
			final = UpdateMin(txfi-1,tzfi,min,tx,tz);
			if (!final && nodeActualZ-1 >= 0)  final = UpdateMin(txfi-1,tzfi-1,min,tx,tz);
			if (!final && nodeActualZ+1 < SCENE_DEPTH)  final = UpdateMin(txfi-1,tzfi+1,min,tx,tz);
		}
		if(txfi+1 < SCENE_WIDTH && !final){
			if(!final)final = UpdateMin(txfi+1,tzfi,min,tx,tz);
			if (!final && nodeActualZ-1 >= 0)  final = UpdateMin(txfi+1,tzfi-1,min,tx,tz);
			if (!final && nodeActualZ+1 < SCENE_DEPTH) final = UpdateMin(txfi+1,tzfi+1,min,tx,tz);
		}
		if (tzfi-1 >= 0 && !final){
			if(!final)final = UpdateMin(txfi,tzfi-1,min,tx,tz);
		}
		if (tzfi+1 < SCENE_DEPTH && !final){
			if(!final)final = UpdateMin(txfi,tzfi+1,min,tx,tz);
		}
		txfi = tx;
		tzfi = tz;
		++iteracions;
		if (iteracions > 700){
			int auxxx = 0;
		}
	}
	
}

void cScene::UpdateDist(int x, int y, float newDist, float &minDist, int &id){
	if (!visitat[x][y]){
		if(dist[x][y] > newDist){
			dist[x][y] = newDist;
		}
		if (dist[x][y] < minDist){
			minDist = dist[x][y];
			id = x*100 + y;
		}
	}
}

bool cScene::UpdateMin(int tx, int tz, float &min, int &mintx, int &mintz){
	if (dist[tx][tz] == 0.0) {
		return true;
	}
	if(dist[tx][tz] < min){
		min = dist[tx][tz];
		mintx = tx;
		mintz = tz;
	}
	return false;
}